// pages/order/index.js
const app = getApp() // 引入app
const _request = require("../../utils/request.js"); // 引入require;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    amount:'',
    consumablesName:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    var amount = options.amount;
    var consumablesName = options.consumablesName;
    this.setData({
      id:id,
      amount: amount,
      consumablesName: consumablesName
    });
  },
  nameblur: function (e) {
    this.setData({
      consumablesName: e.detail.value
    })
  },

  handlePrice(e) {
    var text = e.detail.value;
    var reg = /^(\.*)(\d+)(\.?)(\d{0,2}).*$/g;
    if (reg.test(text)) { //正则匹配通过，提取有效文本
      text = text.replace(reg, '$2$3$4');
    } else {
      text = '';
    }
    return text;
  },

  onSave:function(){
    let id = this.data.id;
    let amount = this.data.amount;
    let consumablesName = this.data.consumablesName;
    if(!consumablesName){
      wx.showToast({
        title: '收费项不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    } else if(!amount){
      wx.showToast({
        title: '价格不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    } else {
      const exp = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
      exp.test(amount)
      console.log(exp.test(amount))
      if (!exp.test(amount)) {
        wx.showToast({
          title: '请输入正确的金额',
          icon: 'none',
          duration: 2000
        })
        return;
      }
      if (amount >= 100000) {
        wx.showModal({
            content: '设置不超过5位数',
            icon: 'none',
            duration: 2000
        })
        return;
      }
        
      wx.setStorageSync('marketadd', JSON.stringify({
        id:id,
        consumablesName: consumablesName,
        amount: amount
      }))

      wx.navigateBack({
        delta: 0
      })
    }

    
  }
})